package org.gdstash.ui.character;

import org.gdstash.character.GDChar;
import org.gdstash.character.GDCharTeleportList;
import org.gdstash.character.GDCharUID;
import org.gdstash.ui.GDStashFrame;
import org.gdstash.ui.util.GDAbstractRiftPane;
import org.gdstash.util.GDMsgFormatter;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import java.util.List;

public class RoTRiftAct1Pane extends GDAbstractRiftPane {
    private JCheckBox cbRogueEncampment;
    private JCheckBox cbColdPlains;
    private JCheckBox cbStonyField;
    private JCheckBox cbDarkWood;
    private JCheckBox cbBlackMarsh;
    private JCheckBox cbCloisterOuter;
    private JCheckBox cbJail;
    private JCheckBox cbCloisterInner;
    private JCheckBox cbCatacombs;
    private int difficulty;
    private boolean changed;

    public RoTRiftAct1Pane(final int difficulty, final int direction) {
        this.difficulty = difficulty;
        this.changed = false;
        this.adjustUI();
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(this.cbRogueEncampment).addComponent(this.cbColdPlains).addComponent(this.cbStonyField).addComponent(this.cbDarkWood).addComponent(this.cbBlackMarsh).addComponent(this.cbCloisterOuter).addComponent(this.cbJail).addComponent(this.cbCloisterInner).addComponent(this.cbCatacombs));
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(this.cbRogueEncampment)).addGroup(layout.createParallelGroup().addComponent(this.cbColdPlains)).addGroup(layout.createParallelGroup().addComponent(this.cbStonyField)).addGroup(layout.createParallelGroup().addComponent(this.cbDarkWood)).addGroup(layout.createParallelGroup().addComponent(this.cbBlackMarsh)).addGroup(layout.createParallelGroup().addComponent(this.cbCloisterOuter)).addGroup(layout.createParallelGroup().addComponent(this.cbJail)).addGroup(layout.createParallelGroup().addComponent(this.cbCloisterInner)).addGroup(layout.createParallelGroup().addComponent(this.cbCatacombs));
        if (direction == 0) {
            layout.setHorizontalGroup(vGroup);
            layout.setVerticalGroup(hGroup);
        } else {
            layout.setHorizontalGroup(hGroup);
            layout.setVerticalGroup(vGroup);
        }
        layout.linkSize(0, this.cbRogueEncampment, this.cbColdPlains);
        layout.linkSize(0, this.cbRogueEncampment, this.cbStonyField);
        layout.linkSize(0, this.cbRogueEncampment, this.cbDarkWood);
        layout.linkSize(0, this.cbRogueEncampment, this.cbBlackMarsh);
        layout.linkSize(0, this.cbRogueEncampment, this.cbCloisterOuter);
        layout.linkSize(0, this.cbRogueEncampment, this.cbJail);
        layout.linkSize(0, this.cbRogueEncampment, this.cbCloisterInner);
        layout.linkSize(0, this.cbRogueEncampment, this.cbCatacombs);
        layout.linkSize(1, this.cbRogueEncampment, this.cbColdPlains);
        layout.linkSize(1, this.cbRogueEncampment, this.cbStonyField);
        layout.linkSize(1, this.cbRogueEncampment, this.cbDarkWood);
        layout.linkSize(1, this.cbRogueEncampment, this.cbBlackMarsh);
        layout.linkSize(1, this.cbRogueEncampment, this.cbCloisterOuter);
        layout.linkSize(1, this.cbRogueEncampment, this.cbJail);
        layout.linkSize(1, this.cbRogueEncampment, this.cbCloisterInner);
        layout.linkSize(1, this.cbRogueEncampment, this.cbCatacombs);
    }

    @Override
    public void adjustUI() {
        Font fntLabel = UIManager.getDefaults().getFont("Label.font");
        Font fntCheck = UIManager.getDefaults().getFont("CheckBox.font");
        if (fntCheck == null) {
            fntCheck = fntLabel;
        }
        Font fntBorder = UIManager.getDefaults().getFont("TitledBorder.font");
        if (fntBorder == null) {
            fntBorder = fntLabel;
        }
        fntLabel = fntLabel.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntCheck = fntCheck.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntBorder = fntBorder.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        final Border lowered = BorderFactory.createEtchedBorder(1);
        final Border raised = BorderFactory.createEtchedBorder(0);
        final Border compound = BorderFactory.createCompoundBorder(raised, lowered);
        final TitledBorder text = BorderFactory.createTitledBorder(compound, GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_ACT1"));
        text.setTitleFont(fntBorder);
        this.setBorder(text);
        if (this.cbRogueEncampment == null) {
            (this.cbRogueEncampment = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbRogueEncampment.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_ROT_01_ROGUE_ENCAMPMENT"));
        this.cbRogueEncampment.setFont(fntCheck);
        if (this.cbColdPlains == null) {
            (this.cbColdPlains = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbColdPlains.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_ROT_01_COLD_PLAINS"));
        this.cbColdPlains.setFont(fntCheck);
        if (this.cbStonyField == null) {
            (this.cbStonyField = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbStonyField.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_ROT_01_STONY_FIELD"));
        this.cbStonyField.setFont(fntCheck);
        if (this.cbDarkWood == null) {
            (this.cbDarkWood = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbDarkWood.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_ROT_01_DARK_WOOD"));
        this.cbDarkWood.setFont(fntCheck);
        if (this.cbBlackMarsh == null) {
            (this.cbBlackMarsh = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbBlackMarsh.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_ROT_01_BLACK_MARSH"));
        this.cbBlackMarsh.setFont(fntCheck);
        if (this.cbCloisterOuter == null) {
            (this.cbCloisterOuter = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbCloisterOuter.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_ROT_01_CLOISTER_OUTER"));
        this.cbCloisterOuter.setFont(fntCheck);
        if (this.cbJail == null) {
            (this.cbJail = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbJail.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_ROT_01_JAIL"));
        this.cbJail.setFont(fntCheck);
        if (this.cbCloisterInner == null) {
            (this.cbCloisterInner = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbCloisterInner.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_ROT_01_CLOISTER_INNER"));
        this.cbCloisterInner.setFont(fntCheck);
        if (this.cbCatacombs == null) {
            (this.cbCatacombs = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbCatacombs.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_ROT_01_CATACOMBS"));
        this.cbCatacombs.setFont(fntCheck);
    }

    @Override
    public boolean hasChanged() {
        return this.changed;
    }

    @Override
    public void setChanged(final boolean changed) {
        this.changed = changed;
    }

    public void setSelected(final boolean selected) {
        this.cbRogueEncampment.setSelected(selected);
        this.cbColdPlains.setSelected(selected);
        this.cbStonyField.setSelected(selected);
        this.cbDarkWood.setSelected(selected);
        this.cbBlackMarsh.setSelected(selected);
        this.cbCloisterOuter.setSelected(selected);
        this.cbJail.setSelected(selected);
        this.cbCloisterInner.setSelected(selected);
        this.cbCatacombs.setSelected(selected);
    }

    @Override
    public List<GDCharUID> getRiftList(final boolean found) {
        final List<GDCharUID> list = new LinkedList<GDCharUID>();
        if (this.cbRogueEncampment.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_ROT_01_ROGUE_ENCAMPMENT);
        }
        if (this.cbColdPlains.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_ROT_01_COLD_PLAINS);
        }
        if (this.cbStonyField.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_ROT_01_STONY_FIELD);
        }
        if (this.cbDarkWood.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_ROT_01_DARK_WOOD);
        }
        if (this.cbBlackMarsh.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_ROT_01_BLACK_MARSH);
        }
        if (this.cbCloisterOuter.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_ROT_01_CLOISTER_OUTER);
        }
        if (this.cbJail.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_ROT_01_JAIL);
        }
        if (this.cbCloisterInner.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_ROT_01_CLOISTER_INNER);
        }
        if (this.cbCatacombs.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_ROT_01_CATACOMBS);
        }
        return list;
    }

    @Override
    public void setChar(final GDChar gdc) {
        this.setSelected(false);
        this.changed = false;
        if (gdc == null) {
            return;
        }
        final List<GDCharUID> list = gdc.getRiftList(this.difficulty);
        for (final GDCharUID uid : list) {
            if (uid.equals(GDCharTeleportList.UID_RIFT_ROT_01_ROGUE_ENCAMPMENT)) {
                this.cbRogueEncampment.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_ROT_01_COLD_PLAINS)) {
                this.cbColdPlains.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_ROT_01_STONY_FIELD)) {
                this.cbStonyField.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_ROT_01_DARK_WOOD)) {
                this.cbDarkWood.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_ROT_01_BLACK_MARSH)) {
                this.cbBlackMarsh.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_ROT_01_CLOISTER_OUTER)) {
                this.cbCloisterOuter.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_ROT_01_JAIL)) {
                this.cbJail.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_ROT_01_CLOISTER_INNER)) {
                this.cbCloisterInner.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_ROT_01_CATACOMBS)) {
                this.cbCatacombs.setSelected(true);
            }
        }
    }

    private class ChangeActionListener implements ActionListener {
        @Override
        public void actionPerformed(final ActionEvent e) {
            RoTRiftAct1Pane.this.changed = true;
        }
    }
}
